package com.ruoyi.receipt.utils;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.util.BytePictureUtils;
import com.ruoyi.receipt.domain.Poic;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

public class WordUtil {
   public String word(List<Poic> list) throws IOException {
      Map<String, List<Map<String, Object>>> datasMap = new HashMap<>();
      List list1 =new ArrayList();
       String path = null;
       String urlPath=null;
     for (Poic poic:list){
         Map map=new HashMap();
         path=poic.getWeChatPicture();
         urlPath=poic.getPicture();
         PictureRenderData pictureRenderData=new PictureRenderData(100,100,path);
         PictureRenderData pictureRenderData1=new PictureRenderData(200,400,".png", BytePictureUtils.getUrlByteArray(urlPath));
         map.put("name",poic.getName());
         map.put("age",poic.getAge());
         map.put("weChatPicture",pictureRenderData);
         map.put("picture",pictureRenderData1);
         list1.add(map);
     }
      datasMap.put("item",list1);
      File file = new File("bp-biz-receipt/src/main/resources/template/template.docx");
      XWPFTemplate template = XWPFTemplate.compile(file).render(datasMap);
      //生成一个名字
       UUID randomUUID = UUID.randomUUID();//生成得文件名字
       //获取系统虚拟路径
       String property = System.getProperty("Java.io.tmpdir");
       //获取时间
       SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
       Date date=new Date(System.currentTimeMillis());
       String format1 = format.format(date);
       File file2=new File(System.getProperty("java.io.tmpdir")+"\\"+format1);
       String absolutePath=null;
       //System.out.println(file2.getAbsolutePath());
       if (!file2.mkdir()){
           file2.createNewFile();
          // System.out.println("没有这个文件夹，现在开始创建文件夹");
           File file1=new File(file2.getAbsolutePath()+"\\"+randomUUID+".docx");
           FileOutputStream out = new FileOutputStream(file1);
           template.write(out);
           out.flush();
           out.close();
           template.close();
          absolutePath = file1.getAbsolutePath();
           System.out.println(absolutePath);
       }else {
         //  System.out.println("已经有这个文件夹了 直接创建文件了");
           File file1=new File(file2.getAbsolutePath()+"\\"+randomUUID+".docx");
           FileOutputStream out = new FileOutputStream(file1);
           template.write(out);
           out.flush();
           out.close();
           template.close();
          absolutePath = file1.getAbsolutePath();
           System.out.println(absolutePath);
       }
      return  absolutePath;
   }
}
